﻿using Abp.Dependency;
using GN.Framework.Utils;
using GN.Pay.Core.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GN.Pay.Core.Services.Impl
{
    /// <summary>
    /// 
    /// </summary>
    class TradeSynchronization : APayService, ITradeSynchronization
    {
        private readonly IIocManager iocManager;
        private readonly ITradeCacheService tradeCacheService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="iocManager"></param>
        /// <param name="tradeCacheService"></param>
        public TradeSynchronization(IIocManager iocManager,
            ITradeCacheService tradeCacheService)
        {
            this.iocManager = iocManager;
            this.tradeCacheService = tradeCacheService;
        }

        /// <summary>
        /// 交易通道
        /// </summary>
        /// <param name="tradeNo">交易号</param>
        public void TradeChannelPay(string tradeNo)
        {
            ExceptionUtils.CheckNotNullAndNotWhiteSpace(tradeNo, nameof(tradeNo));
            var applyRepository = iocManager.Resolve<ITradePayApplyRepository>();
            var channelRepository = iocManager.Resolve<ITradePayApplyChannelRepository>();
            var channelInfo = channelRepository.Get(tradeNo);
            if (channelInfo != null)
            {
                var apply = channelInfo.TradePayApply;
                var channel = tradeCacheService.GetTradeChannel(apply.AppId, channelInfo.ChannelId);
                if (channel != null)
                {
                    var order = channel.OrderSingleQuery(apply.ApplyTime, channelInfo.Id, "");
                    if (order != null)
                    {
                        var notify = iocManager.Resolve<ITradeNotifyService>();
                        Task.Run(() =>
                        {
                            notify.PayNotify(order);
                        });
                    }
                    else
                    {
                        //已过期
                        if (channelInfo.TradePayApply.IsExpire() && channel.OrderSingleQueryIsReliable)
                        {
                            if (apply.TradePayApplyChannels.Count == 1)
                            {
                                applyRepository.Delete(apply);
                            }
                            else
                            {
                                channelRepository.Delete(channelInfo);
                            }
                            applyRepository.SaveChanges();
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 异步交易同步
        /// </summary>
        /// <param name="tradeNo">交易号</param>
        /// <returns></returns>
        public Task TradeChannelPayAsync(string tradeNo)
        {
            return Task.Run(() =>
            {
                TradeChannelPay(tradeNo);
            });
        }
    }
}
